package com.example.demo.service;
import com.example.demo.converter.OutfitConverter;
import com.example.demo.dao.Cloth;
import com.example.demo.dao.ClothRepository;
import com.example.demo.dao.Outfit;
import com.example.demo.dao.OutfitRepository;
import com.example.demo.dto.OutfitDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Objects;
import java.util.Set;

@Service
public class OutfitServiceImpl implements OutfitService {
    @Autowired
    private OutfitRepository outfitRepository;
    @Autowired
    private UserService userService;
    @Autowired
    private ClothRepository clothRepository;

    @Override
    @Transactional
    public Long addClothToOutfitByUserIdAndUserClothIdAndName(String name, int userClothId, long userId) {
        Outfit outfit = outfitRepository.findByUserIdAndName(userId, name);
        Cloth cloth = clothRepository.findByUserIdAndUserClothId(userId, userClothId);

        outfit.getClothes().add(cloth);
        outfitRepository.save(outfit);
        return (long) outfit.getClothes().size();
    }

//    @Override
//    public Long addClothToOutfitByUserIdAndName(String name, long userId, ClothDTO clothDTO) {
//        Outfit outfit = outfitRepository.findByUserIdAndName(userId, name);
//        outfit.getClothes().add(ClothConverter.convertCloth(clothDTO));
//        outfitRepository.save(outfit);
//        return (long) outfit.getClothes().size();
//    }

    @Override
    public Set<OutfitDTO> getOutfitByUserId(long userId) {
        return OutfitConverter.convertToOutfitDTOSet(outfitRepository.findByUserId(userId));
    }

    @Override
    public OutfitDTO getOutfitByUserIdAndName(long userId, String name) {
        return OutfitConverter.convertOutfit(outfitRepository.findByUserIdAndName(userId, name));
    }

    @Transactional
    @Override
    public Set<OutfitDTO> getOutfitByUserIdAndCategory(long userId, String category) {
        return OutfitConverter.convertToOutfitDTOSet(outfitRepository.findByUserIdAndCategory(userId, category));
    }
    @Override
    public OutfitDTO updateOutfitByUserIdAndName(long userId, String name, String description) {
        Outfit outfitInDB = outfitRepository.findByUserIdAndName(userId, name);
        if (outfitInDB != null) {
            if (StringUtils.hasLength(description) && !Objects.equals(outfitInDB.getDescription(), description)) {
                outfitInDB.setDescription(description);
            }
            Outfit outfit = outfitRepository.save(outfitInDB);
            return OutfitConverter.convertOutfit(outfit);
        } else {
            throw new RuntimeException("outfit not found");
        }
    }


    @Transactional
    @Override
    public void deleteOutfitByUserIdAndName(long userId, String name) {
        outfitRepository.deleteOutfitByUserIdAndName(userId, name);
    }

    @Override
    public Long addOutfitByUserId(OutfitDTO outfitDTO, long userId) {
        outfitDTO.setUserDTO(userService.getUserById(userId));
        outfitRepository.save(OutfitConverter.convertOutfit(outfitDTO));
        return outfitDTO.getId();
    }
}
